<?php
class Administrator_seoreportController extends Zend_Controller_Action
{
	public function init() {
	}
	
	public function indexAction(){
		$this->getWorkingReport(20);
		$this->getNewsReport(20);
		
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/seoreport/index.js');
		$this->view->inlineScript()->appendFile(WEB_PATH.'/public/admin/js/hightchart/js/highcharts.js');
		$this->view->inlineScript()->appendFile(WEB_PATH.'/public/admin/js/hightchart/js/modules/exporting.js');
	}
	
	private function getWorkingReport($numDate = 20){
		$objSeoReport 	= new HT_Model_administrator_models_seoreport();
		$objSeoer 		= new HT_Model_administrator_models_seoer();
		$objUtil 	  	= new HT_Model_administrator_models_utility();
	
		$from_date 			= time() - $numDate*24*60*60;
		$from_date			= date('Y-m-d',$from_date);
		$to_date 			= date('Y-m-d');
			
		$filter					= array();
		$filter['from_date']	= $from_date;
		$filter['to_date']		= $to_date;
		
		$listSeoer		= $objSeoer->getListSeoer(0,9999,array('seoer_status'=>1));
		$adminList		= array();
		foreach((array)$listSeoer as $seoer){
			$adminList[]	= $seoer['wishid'];
		}
			
		$dataSeo 				= null;
		$dataNews 				= null;
		$categories 			= null;

		$dateList = array();
		for($i=$numDate;$i >=0;$i--){
			$reportDate 		=  date('Y-m-d',time() - $i*24*60*60);
			$categories 		.= "'".$objUtil->reportDate($reportDate)."',";
			$dateList[] 		=  $reportDate;
		}		
		
		$workingReport 		= $objSeoReport->countWorkingList($adminList,$dateList);
		$workingReport		= $this->_renderWorkingReport($workingReport,$categories);
		
		$this->view->workingReport = $workingReport;
	}
	
private function _renderWorkingReport($workingList,$categories){
		$return = '<script type="text/javascript">'."\r\n";
		$return .= '$(function () {'."\r\n";
			$return .= "$('#working_report').highcharts({"."\r\n";
				$return .= 'chart: {'."\r\n";
					$return .= "type: 'line',"."\r\n";
					$return .= 'marginRight: 130,'."\r\n";
					$return .= 'marginBottom: 25'."\r\n";
				$return .= '},'."\r\n";
				$return .= 'title: {'."\r\n";
					$return .= "text: 'Biểu đồ tiến độ công việc nhóm SEO',"."\r\n";
					$return .= 'x: -20'."\r\n";
				$return .= '},'."\r\n";
				$return .= 'subtitle: {'."\r\n";
					$return .= "text: 'Nguồn WiSH.vn',"."\r\n";
					$return .= 'x: -20'."\r\n";
				$return .= '},'."\r\n";
				$return .= 'xAxis: {'."\r\n";
			                $return .= 'categories: ['.$categories.']'."\r\n";
			            $return .= '},'."\r\n";
			            $return .= 'yAxis: {'."\r\n";
			                $return .= 'title: {'."\r\n";
			                    $return .= "text: 'Báo cáo tiến độ công việc nhóm SEO'"."\r\n";
			                $return .= '},'."\r\n";
			                $return .= 'plotLines: [{'."\r\n";
			                    $return .= 'value: 0,'."\r\n";
			                    $return .= 'width: 1,'."\r\n";
			                    $return .= "color: '#808080'"."\r\n";
			                $return .= '}]'."\r\n";
			            $return .= '},'."\r\n";
			            $return .= 'tooltip: {'."\r\n";
			                $return .= "valueSuffix: ' tin'"."\r\n";
			            $return .= '},'."\r\n";
			            $return .= 'legend: {'."\r\n";
			                $return .= "layout: 'vertical',"."\r\n";
			                $return .= "align: 'right',"."\r\n";
			                $return .= "verticalAlign: 'top',"."\r\n";
			                $return .= 'x: -10,'."\r\n";
			                $return .= 'y: 100,'."\r\n";
			                $return .= 'borderWidth: 0'."\r\n";
			            $return .= '},'."\r\n";
			            $return .= 'series: ['."\r\n";
			            
			            foreach((array)$workingList as $item){
			            	$return .= "{name: '".$item['adminName']."',data: [".$item['data']."]},"."\r\n";
			            }
			            $return .= ']'."\r\n";
			        $return .= '});'."\r\n";
			    $return .= '});'."\r\n";
			$return .= '</script>'."\r\n";
			
			return $return;
	}
	
	private function getNewsReport($numDate = 20){
		$objSeoReport = new HT_Model_administrator_models_seoreport();
		$objUtil 	  = new HT_Model_administrator_models_utility();
		
		$from_date 			= time() - $numDate*24*60*60;
		$from_date			= date('Y-m-d',$from_date);
		$to_date 			= date('Y-m-d');
			
		$filter					= array();
		$filter['from_date']	= $from_date;
		$filter['to_date']		= $to_date;
			
		$seoList 	= $objSeoReport->countSeoList($filter);
		$newsList 	= $objSeoReport->countNewsList($filter);
		$dataSeo = null;
		$dataNews = null;
		$categories = null;
			
		for($i=$numDate;$i >=0;$i--){
			$reportDate 	=  date('Y-m-d',time() - $i*24*60*60);
			$categories 	.= "'".$objUtil->reportDate($reportDate)."',";
			if(isset($seoList[$reportDate])){
				$dataSeo		.= $seoList[$reportDate].',';
			}else{
				$dataSeo		.= '0,';
			}
		
			if(isset($newsList[$reportDate])){
				$dataNews		.= $newsList[$reportDate].',';
			}else{
				$dataNews		.= '0,';
			}
		}
		$this->view->categories = $categories;
		$this->view->dataSeo = $dataSeo;
		$this->view->dataNews = $dataNews;
	}
	
}
